//
//  LocationMessageTableViewCell.m
//  TQY
//
//  Created by 甘世清 on 2024/10/28.
//

#import "LocationMessageTableViewCell.h"
#import "LockMapViewController.h"
@interface LocationMessageTableViewCell()
@property (nonatomic, strong) UILabel *nameLab;//名字
@end
@implementation LocationMessageTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (void)SQ_Style:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
     self.contentView.backgroundColor = UIColor.clearColor;
    [self.contentView addSubview:self.showImage];
    UIView * bgView = [[UIView alloc]init];
    bgView.backgroundColor = UIColor.whiteColor;
    [self.showImage addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.equalTo(self.showImage);
            make.height.mas_equalTo(30);
    }];
    [bgView addSubview:self.nameLab];
    [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(bgView).insets(UIEdgeInsetsMake(5, 10, 5, 10));
    }];
    [self.contentView addSubview:self.hudView];
    [self.hudView addSubview:self.hud];
    [self.hudView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(20);
        make.right.equalTo(self.showImage.mas_left).offset(-2);
        make.bottom.equalTo(self.showImage.mas_bottom);
    }];
    UILongPressGestureRecognizer *longPressGr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressToDo)];
    longPressGr.minimumPressDuration = 0.5;
    [self.showImage addGestureRecognizer:longPressGr];
    
     self.FS_cellBottomView = self.showImage;
}

- (void)setModel:(IMMessageModel *)model{
    [super setModel:model];
    self.nameLab.text = model.address;
    [self.showImage.headerImage sd_setImageWithURL:SQURLImage(model.content) placeholderImage:SQGetImage(@"PhotoDownloadfailed")];
    self.showImage.frame = CGRectMake(model.isSend?SCREEN_WIDETH-SCREEN_WIDETH*0.618f-65:65, self.headerView.y, SCREEN_WIDETH*0.618f, (SCREEN_WIDETH*0.618f)*0.618);
    if ([self.model.needToSend isEqualToString:@"需要发送"]&&self.model.isSend) {
          [self SQ_SendImageMessage];
    }else{
        [self.hud stopAnimating];
        self.hud.hidden = YES;
    }
    [self.contentView layoutIfNeeded];
}
-(void)SQ_SendImageMessage{
    self.model.needToSend = @"发送中";
    self.hud.hidden = NO;
    [self.hud startAnimating];
    NSMutableDictionary * extend = [[NSMutableDictionary alloc]init];
    [extend setObject:@(self.model.lat) forKey:@"lat"]; //经纬达
    [extend setObject:@(self.model.lng) forKey:@"lng"];//经纬达
    [extend setObject:self.model.address forKey:@"address"];//地址
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:self.model.toUser forKey:@"recvId"];
    [dic setObject:@"5" forKey:@"type"];
    [dic setObject:[RSA encryptString:self.model.content publicKey:PUBLICKey] forKey:@"content"];
    [dic setObject:[extend mj_JSONString] forKey:@"extend"];
    [HttpTool postJson:@"im/message/private/send" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        [self.hud stopAnimating];
        self.hud.hidden = YES;
        NSDictionary * datas = [[NSDictionary alloc]initWithDictionary:httpModel.data];
        self.model.needToSend = @"发送成功";
        self.model.messageId = [NSString stringWithFormat:@"%@",[datas objectForKey:@"id"]];
        if (self.SQ_menuTouchActionBlock) {
            self.SQ_menuTouchActionBlock(@"更新会话");
        }
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        self.model.needToSend = @"发送失败";
        [self.hud stopAnimating];
        self.hud.hidden = YES;
        if (self.SQ_menuTouchActionBlock) {
            self.SQ_menuTouchActionBlock(@"更新会话");
        }
    }];
}

-(void)SQ_lockImageViewButtonClick{
    LockMapViewController * vc = [[LockMapViewController alloc]init];
    vc.model = self.model;
    [[Manager SQ_getCurrentViewController].navigationController pushViewController:vc animated:YES];
}
-(void)longPressToDo{
    // 显示菜单栏
    SJPopMenuItem *delete = [SJPopMenuItem itemWithType:SJPopMenuItemDelete];
    NSMutableArray *items = [NSMutableArray array];
    [items addObject:delete];//复制
    SJPopMenu *menu = [SJPopMenu menu];
    [menu showBy:self.showImage withItems:items];
    __weak typeof(self) weakSelf = self;
    menu.itemActions = ^(SJPopMenuItemType type, NSString *title) {
        switch (type) {
            case SJPopMenuItemWithdraw: //撤回
                
                break;
            case SJPopMenuItemDelete: //删除
                if (self.SQ_menuTouchActionBlock) {
                    self.SQ_menuTouchActionBlock(@"删除本地");
                }
                break;
            default:
                break;
        }
    };
}

- (HeaderView *)showImage{
    if (!_showImage) {
        _showImage= [[HeaderView alloc]init];
        _showImage.headerImage.contentMode = UIViewContentModeScaleAspectFill;
        _showImage.layer.cornerRadius = 4;
        [_showImage.btn addTarget:self action:@selector(SQ_lockImageViewButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _showImage;
}

- (UILabel *)nameLab{
    if (!_nameLab) {
        _nameLab = [[UILabel alloc]init];
        _nameLab.textColor = SQColor666666;
        _nameLab.font = FONT_REGULAR(12);
        _nameLab.adjustsFontSizeToFitWidth  = YES;
        _nameLab.backgroundColor = UIColor.clearColor;
    }
    return _nameLab;
}
@end
